<template>
  <!-- eslint-disable vue/no-multiple-template-root -->
  <!-- #ifndef XT_PC -->
  <u-popup
    :show="innerShow"
    :round="10"
    mode="bottom"
    :close-on-click-overlay="false"
  >
    <view class="x-pa-lg">
      <view
        class="x-fw-lg"
        style="margin-bottom:20px"
      >
        {{ title }}
      </view>
      <view style="margin-bottom:20px">
        <view class="x-text-light x-mb-lg">
          {{ desc1 }}
        </view>
        <view
          class="x-mb-lg"
          style="color:blue"
          @click="openPrivacyContract"
        >
          {{ urlTitle }}
        </view>
        <view class="x-text-light">
          {{ desc2 }}
        </view>
      </view>
      <view class="x-flexp-jcai">
        <!-- bindtap="handleDisagree" -->
        <button
          id="disagree-btn"
          type="default"
          class="x-mr"
          size="mini"
          @click="handleDisagree"
        >
          不同意
        </button>
        <button
          id="agree-btn"
          type="default"
          open-type="agreePrivacyAuthorization"
          class="weui-btn"
          size="mini"
          @agreeprivacyauthorization="handleAgree"
        >
          同意
        </button>
      </view>
    </view>
  </u-popup>
  <!-- #endif -->
</template>

<script>
  export default {
    props: {
      size: {
        type: String,
        default: 'small'
      },
    },
    data() {
      return {
        title: "微信小程序用户隐私保护提示",
        desc1: "感谢您使用本应用，您使用本应用前应当阅井同意",
        urlTitle: "《用户隐私保护指引》",
        desc2: "当您点击同意并开始时用产品服务时，即表示你已理解并同息该条款内容，该条款将对您产生法律约束力。如您拒绝，将无法使用部分功能。",
        innerShow: false,
      }
    },
    methods: {
      getNeedAuth() {
        return new Promise((resolve, reject)=>{
          if (wx.getPrivacySetting) {
            wx.getPrivacySetting({
              success: res => {
                // console.log('隐私协议getPrivacySetting',res) // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
                if (res.needAuthorization) {
                  this.urlTitle = res.privacyContractName
                  // console.log('需要 needAuthorization')
                  // 需要弹出隐私协议
                  this.popUp()
                  this.onHandleAgree = (isAgree)=>{
                    return resolve(isAgree)
                  }
                } else {
                  resolve(true)
                }
              },
              fail: err => {
                this.$x.notify.showToast('获取隐私协议信息失败'+err)
                reject('获取隐私协议失败')
              },
              // complete: () => {},
            })
          } else {
            // 基础库版本过低
            return resolve(true)
          }
        })
      },
      handleDisagree(e) {
        this.disPopUp()
        this.onHandleAgree(false)
      },
      handleAgree(e) {
        this.disPopUp()
        this.onHandleAgree(true)
      },
      popUp() {
        this.innerShow = true
      },
      disPopUp() {
        this.innerShow = false
      },
      openPrivacyContract() {
        wx.openPrivacyContract({
          success: res => {
            // console.log('openPrivacyContract success')
          },
          fail: res => {
            console.error('openPrivacyContract fail', res)
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>
